package pl.alef.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.perf4j.aop.Profiled;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import pl.alef.mindplay.model.Visit;
import pl.alef.mindplay.services.VisitService;

@Controller
public class IndexController {

	@Autowired
	protected VisitService visitService;
	
	protected Logger logger = LoggerFactory.getLogger(IndexController.class);
	
	@Profiled(tag="web:index:index")
	@RequestMapping("/index")
	public ModelAndView index(HttpServletRequest request) {
		logger.info("z loggera slf4j {}", visitService );
		
		ModelAndView mv = new ModelAndView("index");
		boolean go = false;
		try {
			List<Visit> news = visitService.list();
			if(news.size() > 3) go = true;
			logger.info("all news = {}", news );
			mv.addObject("news", news);			
			
		} catch (Exception e) {			
			logger.error("Service call failed.", e);
		}
		//if(go) throw new NullPointerException("bo tak");
		return mv;
	}
	
	
	
}
